Imports System.Collections.Generic
Imports System.IO
Imports System.Runtime.InteropServices

Namespace provider
	Public Class FileSysDataProvider
		Implements IAutoCompleteDataProvider
		Implements IAutoAppendDataProvider
		<StructLayout(LayoutKind.Sequential, CharSet := CharSet.Auto)> _
		Private Structure WIN32_FIND_DATA
			Public dwFileAttributes As FileAttributes
			Public ftCreationTime As FILETIME
			Public ftLastAccessTime As FILETIME
			Public ftLastWriteTime As FILETIME
			Public nFileSizeHigh As UInteger
			Public nFileSizeLow As UInteger
			Public dwReserved0 As UInteger
			Public dwReserved1 As UInteger
			<MarshalAs(UnmanagedType.ByValTStr, SizeConst := 260)> _
			Public cFileName As String
			<MarshalAs(UnmanagedType.ByValTStr, SizeConst := 14)> _
			Public cAlternateFileName As String
		End Structure
		<DllImport("kernel32.dll", CharSet := CharSet.Auto)> _
		Private Shared Function FindFirstFile(lpFileName As String, ByRef lpFindFileData As WIN32_FIND_DATA) As IntPtr
		End Function
		<DllImport("kernel32.dll", CharSet := CharSet.Auto)> _
		Private Shared Function FindNextFile(hFindFile As IntPtr, ByRef lpFindFileData As WIN32_FIND_DATA) As Boolean
		End Function
		<DllImport("kernel32.dll")> _
		Public Shared Function FindClose(hFindFile As IntPtr) As Boolean
		End Function
		Const INVALID_HANDLE_VALUE As Short = -1

		'
		Public Property IncludeFiles() As Boolean
			Get
				Return m_IncludeFiles
			End Get
			Set
				m_IncludeFiles = Value
			End Set
		End Property
		Private m_IncludeFiles As Boolean

		Public Function GetItems(textPattern As String) As IEnumerable(Of String) Implements IAutoCompleteDataProvider.GetItems
			If textPattern.Length < 2 OrElse textPattern(1) <> ":"C Then
                Exit Function
			End If
			Dim lastSlashPos As Integer = textPattern.LastIndexOf("\"C)
			If lastSlashPos = -1 Then
                Exit Function
			End If
			Dim fileNamePatternLength As Integer = textPattern.Length - lastSlashPos - 1
			Dim baseFolder As String = textPattern.Substring(0, lastSlashPos + 1)
			Dim fd As WIN32_FIND_DATA
			Dim hFind = FindFirstFile(textPattern & "*", fd)
			If hFind.ToInt32() = INVALID_HANDLE_VALUE Then
                Exit Function
			End If
			Do
				If fd.cFileName(0) = "."C Then
					Continue Do
				End If
				If (fd.dwFileAttributes And FileAttributes.Hidden) <> 0 Then
					Continue Do
				End If
				If (fd.dwFileAttributes And FileAttributes.Directory) = 0 AndAlso Not IncludeFiles Then
					Continue Do
				End If
				If fileNamePatternLength > fd.cFileName.Length Then
					Continue Do
				End If
                Return baseFolder & fd.cFileName
			Loop While FindNextFile(hFind, fd)
			FindClose(hFind)
		End Function

		Public Function GetAppendText(textPattern As String, firstMatch As String) As String Implements IAutoAppendDataProvider.GetAppendText
			If textPattern.EndsWith("\") Then
				Return Nothing
			End If
			Return firstMatch.Substring(textPattern.Length)
		End Function
	End Class
End Namespace
